package model;

import java.io.File;
import java.util.Observable;

/**
 * 
 * @author Michael & Pierre
 *
 */
/**
 * Model observable used to manage the sharing of files
 */
public class SharingFiles extends Observable{
	
	/**
	 * True if the last modification comes from the local user
	 */
	private boolean waitRemoteAnswer; //vrai si la dernière modif vient de GUI  
	/**
	 * True if the file comes from the local system, false if the file comes from a remote user
	 */
	private boolean isToSend;
	/**
	 * True if the file has been accepted by the recipient
	 */
	private boolean isAccepted;
	/**
	 * True if the file has been sent or receive
	 */
	private boolean isSent;
	/**
	 * Name of the file (used only for received file)
	 */
	private String fileName; //only for received file
	/**
	 * File which will be sent (used only for the files to send)
	 */
	private File fileToSend; //only if isTosend
	/**
	 * Directory where the file will be stored (only for received file)
	 */
	private String destDirectory; //only for received file
	/**
	 * user which will send his file or receive a file from the local system
	 */
	private User user;
	
	/**
	 * Method called when the local user wants to send a file
	 * @param fileToSend file the local user wants to send
	 * @param user recipient of the file
	 */
	public void newSendNotif(File fileToSend, User user){
		this.waitRemoteAnswer = true;
		this.isToSend = true;
		this.isAccepted = false;
		this.isSent = false;
		this.fileToSend = fileToSend;
		this.fileName = fileToSend.getName();
		this.destDirectory = null;
		this.user = user;
		setChanged();
		notifyObservers();
	}
	
	/**
	 * Method called when a remote user wants to share a file with the local user
	 * @param fileName name of the file
	 * @param user user who wants to send a file
	 */
	public void newRcvdNotif(String fileName, User user){
		this.waitRemoteAnswer = false;
		this.isToSend = false;
		this.isAccepted = false;
		this.isSent = false;
		this.fileName = fileName;
		this.fileToSend = null;
		this.destDirectory = null;
		this.user = user;
		setChanged();
		notifyObservers();
	}
	
	/**
	 * Method called when the local user accepted or refused to receive a file
	 * @param isAccepted 
	 * @param destDirectory directory where the accepted file will be stored
	 */
	public void localResponseNotif(boolean isAccepted, String destDirectory){
		this.waitRemoteAnswer = true;
		this.isAccepted = isAccepted;
		this.destDirectory = destDirectory;
		setChanged();
		notifyObservers();
                
	}
	
	/**
	 * Method called when the remote user accepted or refused to receive a file
	 * @param isAccepted
	 */
	public void remoteResponseNotif(boolean isAccepted){
		this.waitRemoteAnswer = false;
		this.isAccepted = isAccepted;
		setChanged();
		notifyObservers();
	}

	/**
	 * Method called when a file has been received
	 */
	public void fileRcvd() {
		this.waitRemoteAnswer = false;
		this.isSent = true;
		setChanged();
		notifyObservers();
	}
	
	/**
	 * Method called when a file has been sent
	 */
	public void fileSent() {
		this.waitRemoteAnswer = false;
		this.isSent = true;
		setChanged();
		notifyObservers();
	}
	
	public boolean getWaitRemoteAnswer () {
		return waitRemoteAnswer;
	}
	
	public boolean isToSend() {
		return isToSend;
	}

	public boolean isAccepted() {
		return isAccepted;
	}

	public boolean isSent() {
		return isSent;
	}
	
    public void setIsSent(boolean isSent) {
    	this.isSent = isSent;
    }
    
	public String getFileName() {
		return fileName;
	}

	public File getFileToSend() {
		return fileToSend;
	}

	public String getDestDirectory() {
		return destDirectory;
	}

	public User getUser() {
		return user;
	}
	
}
